﻿#include "StyleComboBox.h"

#include <QDebug>
#include <QLabel>
#include <QPainter>
#include <QHBoxLayout>
#include <QListWidget>

#include "qcustomplot.h"

namespace pst
{

    LineStyleComboBox::LineStyleComboBox(QWidget* parent)
    {
        m_label = new QLabel(this);
        QHBoxLayout* hBoxLayout = new QHBoxLayout(this);
        hBoxLayout->addWidget(m_label);
        hBoxLayout->setSpacing(0);
        hBoxLayout->setContentsMargins(5, 0, 20, 0);
        this->setLayout(hBoxLayout);
        m_listWidget = new QListWidget(this);
        this->setModel(m_listWidget->model());
        this->setView(m_listWidget);
        for (int i = 0; i < 5; ++i)
        {
            QListWidgetItem* item = new QListWidgetItem(m_listWidget);
            QLabel* label = new QLabel();
            QPixmap pixmap(160, 16);
            pixmap.fill(Qt::transparent);
            QPainter painter(&pixmap);
            painter.setPen(Qt::PenStyle(i + 1));
            painter.drawLine(0, 8, 160, 8);
            label->setPixmap(pixmap);
            m_listWidget->addItem(item);
            m_listWidget->setItemWidget(item, label);
        }
        this->setCurrentIndex(0);
        m_label->setPixmap(*qobject_cast<QLabel*>(m_listWidget->itemWidget(m_listWidget->item(0)))->pixmap());
        //this->show();
        connect(this, QOverload<int>::of(&LineStyleComboBox::currentIndexChanged), [this](int index) {
            if (index >= 0)
            {
                m_label->setPixmap(*qobject_cast<QLabel*>(m_listWidget->itemWidget(m_listWidget->item(index)))->pixmap());
            }
            });
    }

    ScatterStyleComboBox::ScatterStyleComboBox(QWidget* parent)
    {
        m_label = new QLabel(this);
        QHBoxLayout* hBoxLayout = new QHBoxLayout(this);
        hBoxLayout->addWidget(m_label);
        hBoxLayout->setSpacing(0);
        hBoxLayout->setContentsMargins(5, 0, 20, 0);
        this->setLayout(hBoxLayout);
        m_listWidget = new QListWidget(this);
        this->setModel(m_listWidget->model());
        this->setView(m_listWidget);
        for (int i = 0; i < 15; ++i)
        {
            QListWidgetItem* item = new QListWidgetItem(m_listWidget);
            QLabel* label = new QLabel();
            QPixmap pixmap(16, 16);
            pixmap.fill(Qt::transparent);
            QCPScatterStyle scatterStyle;
            scatterStyle.setShape(QCPScatterStyle::ScatterShape(i + 1));
            QCPPainter painter(&pixmap);
            scatterStyle.drawShape(&painter, 8, 8);
            label->setPixmap(pixmap);
            m_listWidget->addItem(item);
            m_listWidget->setItemWidget(item, label);
        }
        this->setCurrentIndex(0);
        m_label->setPixmap(*qobject_cast<QLabel*>(m_listWidget->itemWidget(m_listWidget->item(0)))->pixmap());
        //this->show();
        connect(this, QOverload<int>::of(&LineStyleComboBox::currentIndexChanged), [this](int index) {
            if (index >= 0)
            {
                m_label->setPixmap(*qobject_cast<QLabel*>(m_listWidget->itemWidget(m_listWidget->item(index)))->pixmap());
            }
            });
    }

    TracerStyleComboBox::TracerStyleComboBox(QWidget* parent)
    {
        m_label = new QLabel(this);
        QHBoxLayout* hBoxLayout = new QHBoxLayout(this);
        hBoxLayout->addWidget(m_label);
        hBoxLayout->setSpacing(0);
        hBoxLayout->setContentsMargins(5, 0, 20, 0);
        this->setLayout(hBoxLayout);
        m_listWidget = new QListWidget(this);
        this->setModel(m_listWidget->model());
        this->setView(m_listWidget);
        for (int i = 0; i < 4; ++i)
        {
            QListWidgetItem* item = new QListWidgetItem(m_listWidget);
            QLabel* label = new QLabel();
            QPixmap pixmap(16, 16);
            pixmap.fill(Qt::transparent);

            QPointF center(8, 8);
            double w = 6 / 2.0;
            QCPPainter painter(&pixmap);
            switch (i)
            {
            case 0:
            {
                painter.drawLine(QLineF(center + QPointF(-w, 0), center + QPointF(w, 0)));
                painter.drawLine(QLineF(center + QPointF(0, -w), center + QPointF(0, w)));
                break;
            }
            case 1:
            {
                painter.drawLine(QLineF(0, center.y(), 16, center.y()));
                painter.drawLine(QLineF(center.x(), 0, center.x(), 16));
                break;
            }
            case 2:
            {
                painter.drawEllipse(center, w, w);
                break;
            }
            case 3:
            {
                painter.drawRect(QRectF(center - QPointF(w, w), center + QPointF(w, w)));
                break;
            }
            }

            //QCPScatterStyle scatterStyle;
            //scatterStyle.setShape(QCPScatterStyle::ScatterShape(i + 1));
            //QCPPainter painter(&pixmap);
            //scatterStyle.drawShape(&painter, 8, 8);
            label->setPixmap(pixmap);
            m_listWidget->addItem(item);
            m_listWidget->setItemWidget(item, label);
        }
        this->setCurrentIndex(0);
        m_label->setPixmap(*qobject_cast<QLabel*>(m_listWidget->itemWidget(m_listWidget->item(0)))->pixmap());
        this->show();
        connect(this, QOverload<int>::of(&LineStyleComboBox::currentIndexChanged), [this](int index) {
            if (index >= 0)
            {
                m_label->setPixmap(*qobject_cast<QLabel*>(m_listWidget->itemWidget(m_listWidget->item(index)))->pixmap());
            }
            });
    }

}
